﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PowerBooking.RSE.Utility.Helpers;
using System.IO;
using System.Data.SQLite;
using PowerBooking.RSE.Domain.Models;

namespace PowerBooking.RSE.UnitTest
{
    [TestClass]
    public class DatabaseTest : Test
    {
        public DatabaseTest() : base() { }

        [TestMethod]
        public void InitDatabase()
        {
            var dataSource = @".\RSE.s3db";
            if (File.Exists(dataSource))
            {
                File.Delete(dataSource);
                SQLiteConnection.CreateFile(dataSource);
            }
            


            using (SQLiteConnection conn = new SQLiteConnection(@"Data Source=.\RSE.s3db;UTF8Encoding=True"))
            {
                conn.Open();
                var sql = @"
CREATE TABLE [PowerBooking_RSE_City] (
[CityID] INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,
[SpiderID] INTEGER  NOT NULL,
[Name] NVARCHAR(16)  NOT NULL,
[Url] VARCHAR(128)  NOT NULL
);

CREATE TABLE [PowerBooking_RSE_Filter] (
[FilterID] INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,
[CityID] INTEGER  NOT NULL,
[Name] NVARCHAR(32)  NOT NULL,
[Url] VARCHAR(128)  NOT NULL
);

CREATE TABLE [PowerBooking_RSE_Restaurant] (
[RestaurantID] INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT,
[CityID] INTEGER  NOT NULL,
[FilterID] INTEGER  NOT NULL,
[Name] NVARCHAR(64)  NOT NULL,
[Url] VARCHAR(128)  NOT NULL,
[MinPerCapita] INTEGER  NOT NULL,
[MaxPerCapita] INTEGER  NOT NULL,
[Address] NVARCHAR(64)  NULL,
[Taste] FLOAT  NOT NULL,
[Environment] FLOAT  NOT NULL,
[Service] FLOAT  NOT NULL,
[RecentOrders] INTEGER  NOT NULL,
[Reviews] INTEGER  NOT NULL,
[PeopleLikeRate] INTEGER  NOT NULL,
[Impress] FLOAT  NOT NULL,
[Type] INTEGER  NOT NULL,
[CuisineTags] NVARCHAR(256)  NULL,
[RegionTags] NVARCHAR(256)  NULL
);

CREATE TABLE [PowerBooking_RSE_Spider] (
[SpiderID] INTEGER  PRIMARY KEY AUTOINCREMENT NOT NULL,
[Name] NVARCHAR(64)  NOT NULL,
[Url] VARCHAR(128)  NOT NULL
);
";
                using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                {
                    command.ExecuteNonQuery();
                    command.Dispose();
                }
                conn.Close();
                conn.Dispose();
            }
        }

        [TestMethod]
        public void DbOperatorTester()
        {
            using (SQLiteConnection conn = new SQLiteConnection(@"Data Source=.\RSE.s3db;UTF8Encoding=True"))
            {
                conn.Open();
                var sql = @"Insert into [PowerBooking_RSE_Spider] (Name,Url) values('Xiaomishu','http://www.xiaomishu.com/citylist');";
                using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                {
                    command.ExecuteNonQuery();
                    command.Dispose();
                }
                conn.Close();
                conn.Dispose();
            }
        }

        //[TestMethod]
        public void StartTester()
        {
            CacheHelper.Set("IsCaptureStop", false, DateTime.Now.AddDays(1));
            var spider = SpiderFactory.CreateSpider("Xiaomishu", "http://www.xiaomishu.com/citylist");
            spider.InitCities(CityFactory.CreateCities(spider.SpiderID, spider.Url));
            var city = spider.Cities.Single(c => c.Url.Contains("www"));
            city.InitFilters(FilterFactory.CreateFilters(spider.SpiderID, city.CityID, city.Url));
            var restaurants = new List<Restaurant>();

            for (var f = 0; f < city.Filters.Count; f++)
            {
                restaurants.AddRange(RestaurantFactory.CreateRestaurants(spider.SpiderID, city.CityID, city.Filters[f].FilterID, city.Url, city.Filters[f].Url));
            }
            city.InitRestaurants(restaurants);
        }
    }
}
